<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--table标题插槽-->
      <template #tableTitle>
        <a-button type="primary" v-auth="['purchase:purchase-pre-bill:add']" :preIcon="IconEnum.ADD" @click="handleAdd">{{
          t('action.create')
        }}</a-button>
        <a-button type="primary" :preIcon="IconEnum.ADD" @click="handleBatchPrint">{{ t('action.print') }}</a-button>
        <a-button type="primary" v-auth="['purchase:purchase-pre-bill:export']" :preIcon="IconEnum.EXPORT" @click="onExportXls">{{
          t('action.export')
        }}</a-button>
        <a-button type="primary" :preIcon="IconEnum.apply" @click="onApply">{{ t('action.apply') }}</a-button>
        <a-button type="primary" :preIcon="IconEnum.revert" @click="onRevert">{{ t('action.revert') }}</a-button>
        <a-dropdown>
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="openPageConfig">
                <Icon :icon="IconEnum.DELETE" />
                {{ t('action.pageConfig') }}
              </a-menu-item>
              <a-menu-item key="2" @click="openPrintTpl">
                <Icon :icon="IconEnum.DELETE" />
                {{ t('action.printTplConfig') }}
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >{{ t('common.setting') }}
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏插槽-->
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #bodyCell="{ column, text }">
        <template v-if="column.dataIndex === 'fileId'">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">{{
            t('action.download')
          }}</a-button>
        </template>
      </template>
    </BasicTable>
    <PurchasePreBillModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="purchase-purchasePreBill" setup>
  import { ref, unref } from 'vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { IconEnum } from '@/enums/appEnum';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { getPage, deleteById, getById, batchDelete, getExportUrl } from './purchasePreBill.api';
  import { columns, searchFormSchema } from './purchasePreBill.data';
  import PurchasePreBillModal from './PurchasePreBillModal.vue';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useRouter } from 'vue-router';
  const [registerModal, { openModal }] = useModal();
  const { t } = useI18n();
  const businessCode = ref<string>('purchaseprebill');
  const subBusinessCode = ref<string>('jxc_purchase_bill_item');
  const tableName = ref<string>('jxc_purchase_pre_bill');
  const moduleCode = ref<string>('purchase');

  const router = useRouter();

  // 列表页面公共参数、方法
  const { onExportXls, tableContext } = useListPage({
    designScope: 'purchasePreBill-template',
    tableProps: {
      title: '采购订单列表',
      api: getPage,
      columns: columns,
      useSearchForm: true,
      size: 'small', //紧凑型表格
      formConfig: {
        labelWidth: 60,
        autoSubmitOnEnter: true,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
      showIndexColumn: true,
    },
    exportConfig: {
      name: '采购订单列表',
      url: getExportUrl,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: t('action.edit'),
        onClick: handleEdit.bind(null, record),
      },
      {
        label: t('action.copyAndAdd'),
        onClick: handleCopy.bind(null, record),
      },
      {
        label: t('action.print'),
        onClick: handlePrint.bind(null, record),
      },
    ];
  }

  /**
   * 下拉操作栏
   * @param record
   */
  function getDropDownAction(record) {
    return [
      {
        label: t('action.detail'),
        onClick: handleDetail.bind(null, record),
      },
      {
        label: t('action.giveup'),
        onClick: handleGiveUp.bind(null, record),
      },
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 打印事件
   */
  function handlePrint() {
    console.log('打印.....');
  }

  /**
   * 批量打印事件
   */
  function handleBatchPrint() {
    console.log('批量打印.....');
  }

  /**
   * 字段设置
   */
  function openPageConfig() {
    console.log('字段设置.....');
    router.push({
      path: '/print/index',
      query: {
        businessCode: businessCode.value,
        subBusinessCode: subBusinessCode.value,
        tableName: tableName.value,
        moduleCode: moduleCode.value,
      },
    });
  }

  /**
   * 打印模板设置
   */
  function openPrintTpl() {
    console.log('打印模板.....');
    router.push({
      path: '/print/index',
      query: {
        businessCode: businessCode.value,
        subBusinessCode: subBusinessCode.value,
        tableName: tableName.value,
        moduleCode: moduleCode.value,
      },
    });
  }
  /**
   * 详情事件
   */
  function handleDetail(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 审核
   */
  function onApply() {
    console.log('审核.....');
  }

  /**
   * 反审核
   */
  function onRevert() {
    console.log('反审核.....');
  }

  /**
   * 作废事件
   */
  async function handleGiveUp(record) {
    await deleteById({ id: record.id }, reload);
  }

  /**
   * 复制事件
   */
  async function handleCopy(record) {
    //record.id = null;
    //todo 掉后台接口获取订单号并赋值
    await openModal(true, {
      record,
      isUpdate: false,
      isCopy: true,
    });
  }
  /**
   * 批量删除事件
   */
  async function handleBatchDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, reload);
  }
</script>
